package com.wondersgroup.fjzlpt.console.service.impl.bqxx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.console.dao.inter.bqxx.BqxxDAO;
import com.wondersgroup.fjzlpt.console.service.inter.bqxx.BqxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by lion on 2017/8/14.
 */
@Service
public class BqxxServiceImpl implements BqxxService {

    @Autowired
    private BqxxDAO bqxxDAO;

    /**
     * 分页查询病区信息
     * @param map
     * @return
     */
    public JSONObject queryPageBqxx(Map map){
        return bqxxDAO.queryPageBqxx(map);
    }

    public JSONObject save(Map map){
        ResultObject ro =  new ResultObject();
        String method = (String)map.remove("method");
        if("add".equals(method)){
            insert(map, ro);
        }else{
            update(map, ro);
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    private void insert(Map map, ResultObject ro){
        try{
            this.bqxxDAO.insert(map);
        }catch (Exception e){
            e.printStackTrace();
            ro.changeFaultState("新增病区失败");
            throw new RuntimeException("新增病区失败", e);
        }
    }

    private void update(Map map, ResultObject ro){
        try{
            this.bqxxDAO.updateForPrimaryKey(map);
        }catch (Exception e){
            e.printStackTrace();
            ro.changeFaultState("修改病区失败");
            throw new RuntimeException("修改病区失败", e);
        }
    }

    public JSONObject delete(Map map){
        ResultObject ro = new ResultObject();
        try{
            this.bqxxDAO.deleteForPrimaryKey(map);
        }catch (Exception e){
            e.printStackTrace();
            ro.changeFaultState("删除病区失败");
            throw new RuntimeException("删除病区失败", e);
        }
        return (JSONObject)JSON.toJSON(ro);
    }
}
